#ifndef OTAMANAGER_H
#define OTAMANAGER_H

#include <Arduino.h>
#include <ArduinoOTA.h>
#include <WiFi.h>
#include "hub75e.h"

class OTAManager {
public:
    OTAManager(Panel& panel);
    
    // Initialize OTA with default settings
    bool begin(const char* hostname = "CalClock-ESP32", const char* password = "calclockota123");
    
    // Handle OTA updates (call in main loop)
    void handle();
    
    // Status checking methods
    bool isInProgress() const { return _otaInProgress; }
    bool hasError() const { return _otaError != ""; }
    String getStatus() const { return _otaStatus; }
    String getError() const { return _otaError; }
    unsigned int getProgress() const { return _otaProgress; }
    
    // Display methods
    void drawStatus();
    void updateDisplay();
    
    // Clear error/status messages
    void clearMessages();

private:
    Panel& _panel;
    
    // OTA state variables
    bool _otaInProgress;
    unsigned int _otaProgress;
    String _otaStatus;
    String _otaError;
    
    // Display timing
    unsigned long _lastOTADraw;
    static const unsigned long OTA_DRAW_INTERVAL = 17; // 60 FPS
    static const unsigned long STATUS_DISPLAY_TIME = 5000; // 5 seconds
    
    // Internal methods
    void setupCallbacks();
    void drawText5x8(uint16_t x, uint16_t y, const char* text, uint8_t r, uint8_t g, uint8_t b);
    
    // OTA callback functions
    static OTAManager* _instance; // For static callbacks
    static void onStartCallback();
    static void onEndCallback();
    static void onProgressCallback(unsigned int progress, unsigned int total);
    static void onErrorCallback(ota_error_t error);
};

#endif // OTAMANAGER_H